<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>pgmpy.sampling.NUTS &#8212; pgmpy 0.1.2 documentation</title>
    
    <link rel="stylesheet" href="../../../_static/sphinxdoc.css" type="text/css" />
    <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../../../',
        VERSION:     '0.1.2',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true,
        SOURCELINK_SUFFIX: '.txt'
      };
    </script>
    <script type="text/javascript" src="../../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../../_static/doctools.js"></script>
    <link rel="index" title="Index" href="../../../genindex.html" />
    <link rel="search" title="Search" href="../../../search.html" /> 
  </head>
  <body role="document">
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="nav-item nav-item-0"><a href="../../../index.html">pgmpy 0.1.2 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="../../index.html" accesskey="U">Module code</a> &#187;</li> 
      </ul>
    </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
            <p class="logo"><a href="../../../index.html">
              <img class="logo" src="../../../_static/logo.png" alt="Logo"/>
            </a></p>
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <form class="search" action="../../../search.html" method="get">
      <div><input type="text" name="q" /></div>
      <div><input type="submit" value="Go" /></div>
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <h1>Source code for pgmpy.sampling.NUTS</h1><div class="highlight"><pre>
<span></span><span class="c1"># -*- coding: utf-8 -*-</span>

<span class="kn">from</span> <span class="nn">__future__</span> <span class="k">import</span> <span class="n">division</span>

<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>

<span class="kn">from</span> <span class="nn">pgmpy.sampling</span> <span class="k">import</span> <span class="n">HamiltonianMCDA</span><span class="p">,</span> <span class="n">LeapFrog</span><span class="p">,</span> <span class="n">_return_samples</span>
<span class="kn">from</span> <span class="nn">pgmpy.utils</span> <span class="k">import</span> <span class="n">_check_1d_array_object</span><span class="p">,</span> <span class="n">_check_length_equal</span>


<div class="viewcode-block" id="NoUTurnSampler"><a class="viewcode-back" href="../../../sampling.html#pgmpy.sampling.NUTS.NoUTurnSampler">[docs]</a><span class="k">class</span> <span class="nc">NoUTurnSampler</span><span class="p">(</span><span class="n">HamiltonianMCDA</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Class for performing sampling in Continuous model</span>
<span class="sd">    using No U Turn Sampler (a variant of Hamiltonian Monte Carlo)</span>

<span class="sd">    Parameters:</span>
<span class="sd">    -----------</span>
<span class="sd">    model: An instance pgmpy.models</span>
<span class="sd">        Model from which sampling has to be done</span>

<span class="sd">    grad_log_pdf: A subclass of pgmpy.inference.continuous.GradientLogPDF</span>
<span class="sd">        Class to compute the log and gradient log of distribution</span>

<span class="sd">    simulate_dynamics: A subclass of pgmpy.inference.continuous.BaseSimulateHamiltonianDynamics</span>
<span class="sd">        Class to propose future states of position and momentum in time by simulating</span>
<span class="sd">        HamiltonianDynamics</span>

<span class="sd">    Public Methods:</span>
<span class="sd">    ---------------</span>
<span class="sd">    sample()</span>
<span class="sd">    generate_sample()</span>

<span class="sd">    Example:</span>
<span class="sd">    --------</span>
<span class="sd">    &gt;&gt;&gt; from pgmpy.sampling import NoUTurnSampler as NUTS, LeapFrog, GradLogPDFGaussian</span>
<span class="sd">    &gt;&gt;&gt; from pgmpy.factors.continuous import GaussianDistribution as JGD</span>
<span class="sd">    &gt;&gt;&gt; import numpy as np</span>
<span class="sd">    &gt;&gt;&gt; mean = np.array([1, 2, 3])</span>
<span class="sd">    &gt;&gt;&gt; covariance = np.array([[4, 0.1, 0.2], [0.1, 1, 0.3], [0.2, 0.3, 8]])</span>
<span class="sd">    &gt;&gt;&gt; model = JGD([&#39;x&#39;, &#39;y&#39;, &#39;z&#39;], mean, covariance)</span>
<span class="sd">    &gt;&gt;&gt; sampler = NUTS(model=model, grad_log_pdf=GradLogPDFGaussian, simulate_dynamics=LeapFrog)</span>
<span class="sd">    &gt;&gt;&gt; samples = sampler.sample(initial_pos=np.array([0.1, 0.9, 0.3]), num_samples=20000,</span>
<span class="sd">    ...                          stepsize=0.4, return_type=&#39;recarray&#39;)</span>
<span class="sd">    &gt;&gt;&gt; samples</span>
<span class="sd">    rec.array([(0.1, 0.9, 0.3),</span>
<span class="sd">     (-0.27303886844752756, 0.5028580705249155, 0.2895768065049909),</span>
<span class="sd">     (1.7139810571103862, 2.809135711303245, 5.690811523613858), ...,</span>
<span class="sd">     (-0.7742669710786649, 2.092867703984895, 6.139480724333439),</span>
<span class="sd">     (1.3916152816323692, 1.394952482021687, 3.446906546649354),</span>
<span class="sd">     (-0.2726336476939125, 2.6230854954595357, 2.923948403903159)],</span>
<span class="sd">              dtype=[(&#39;x&#39;, &#39;&lt;f8&#39;), (&#39;y&#39;, &#39;&lt;f8&#39;), (&#39;z&#39;, &#39;&lt;f8&#39;)])</span>

<span class="sd">    References</span>
<span class="sd">    ----------</span>
<span class="sd">    Matthew D. Hoffman, Andrew Gelman, The No-U-Turn Sampler: Adaptively</span>
<span class="sd">    Setting Path Lengths in Hamiltonian Monte Carlo. Journal of</span>
<span class="sd">    Machine Learning Research 15 (2014) 1351-1381</span>
<span class="sd">    Algorithm 3 : Efficient No-U-Turn Sampler</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">model</span><span class="p">,</span> <span class="n">grad_log_pdf</span><span class="p">,</span> <span class="n">simulate_dynamics</span><span class="o">=</span><span class="n">LeapFrog</span><span class="p">):</span>

        <span class="nb">super</span><span class="p">(</span><span class="n">NoUTurnSampler</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">__init__</span><span class="p">(</span><span class="n">model</span><span class="o">=</span><span class="n">model</span><span class="p">,</span> <span class="n">grad_log_pdf</span><span class="o">=</span><span class="n">grad_log_pdf</span><span class="p">,</span>
                                             <span class="n">simulate_dynamics</span><span class="o">=</span><span class="n">simulate_dynamics</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_initalize_tree</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">position</span><span class="p">,</span> <span class="n">momentum</span><span class="p">,</span> <span class="n">slice_var</span><span class="p">,</span> <span class="n">stepsize</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Initalizes root node of the tree, i.e depth = 0</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="n">position_bar</span><span class="p">,</span> <span class="n">momentum_bar</span><span class="p">,</span> <span class="n">_</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">simulate_dynamics</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="p">,</span> <span class="n">position</span><span class="p">,</span> <span class="n">momentum</span><span class="p">,</span> <span class="n">stepsize</span><span class="p">,</span>
                                                               <span class="bp">self</span><span class="o">.</span><span class="n">grad_log_pdf</span><span class="p">)</span><span class="o">.</span><span class="n">get_proposed_values</span><span class="p">()</span>

        <span class="n">_</span><span class="p">,</span> <span class="n">logp_bar</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">grad_log_pdf</span><span class="p">(</span><span class="n">position_bar</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="p">)</span><span class="o">.</span><span class="n">get_gradient_log_pdf</span><span class="p">()</span>

        <span class="n">hamiltonian</span> <span class="o">=</span> <span class="n">logp_bar</span> <span class="o">-</span> <span class="mf">0.5</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">dot</span><span class="p">(</span><span class="n">momentum_bar</span><span class="p">,</span> <span class="n">momentum_bar</span><span class="p">)</span>

        <span class="n">candidate_set_size</span> <span class="o">=</span> <span class="n">slice_var</span> <span class="o">&lt;</span> <span class="n">np</span><span class="o">.</span><span class="n">exp</span><span class="p">(</span><span class="n">hamiltonian</span><span class="p">)</span>
        <span class="n">accept_set_bool</span> <span class="o">=</span> <span class="n">hamiltonian</span> <span class="o">&gt;</span> <span class="n">np</span><span class="o">.</span><span class="n">log</span><span class="p">(</span><span class="n">slice_var</span><span class="p">)</span> <span class="o">-</span> <span class="mi">10000</span>  <span class="c1"># delta_max = 10000</span>

        <span class="k">return</span> <span class="n">position_bar</span><span class="p">,</span> <span class="n">momentum_bar</span><span class="p">,</span> <span class="n">candidate_set_size</span><span class="p">,</span> <span class="n">accept_set_bool</span>

    <span class="k">def</span> <span class="nf">_update_acceptance_criteria</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">position_forward</span><span class="p">,</span> <span class="n">position_backward</span><span class="p">,</span> <span class="n">momentum_forward</span><span class="p">,</span> <span class="n">momentum_backward</span><span class="p">,</span>
                                    <span class="n">accept_set_bool</span><span class="p">,</span> <span class="n">candidate_set_size</span><span class="p">,</span> <span class="n">candidate_set_size2</span><span class="p">):</span>

        <span class="c1"># criteria1 = I[(θ+ − θ−)·r− ≥ 0]</span>
        <span class="n">criteria1</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">dot</span><span class="p">((</span><span class="n">position_forward</span> <span class="o">-</span> <span class="n">position_backward</span><span class="p">),</span> <span class="n">momentum_backward</span><span class="p">)</span> <span class="o">&gt;=</span> <span class="mi">0</span>

        <span class="c1"># criteira2 = I[(θ+ − θ− )·r+ ≥ 0]</span>
        <span class="n">criteria2</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">dot</span><span class="p">((</span><span class="n">position_forward</span> <span class="o">-</span> <span class="n">position_backward</span><span class="p">),</span> <span class="n">momentum_forward</span><span class="p">)</span> <span class="o">&gt;=</span> <span class="mi">0</span>

        <span class="n">accept_set_bool</span> <span class="o">=</span> <span class="n">accept_set_bool</span> <span class="ow">and</span> <span class="n">criteria1</span> <span class="ow">and</span> <span class="n">criteria2</span>
        <span class="n">candidate_set_size</span> <span class="o">+=</span> <span class="n">candidate_set_size2</span>

        <span class="k">return</span> <span class="n">accept_set_bool</span><span class="p">,</span> <span class="n">candidate_set_size</span>

    <span class="k">def</span> <span class="nf">_build_tree</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">position</span><span class="p">,</span> <span class="n">momentum</span><span class="p">,</span> <span class="n">slice_var</span><span class="p">,</span> <span class="n">direction</span><span class="p">,</span> <span class="n">depth</span><span class="p">,</span> <span class="n">stepsize</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Recursively builds a tree for proposing new position and momentum</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="c1"># Parameter names in algorithm (here -&gt; representation in algorithm)</span>
        <span class="c1"># position -&gt; theta, momentum -&gt; r, slice_var -&gt; u, direction -&gt; v, depth -&gt;j, stepsize -&gt; epsilon</span>
        <span class="c1"># candidate_set_size -&gt; n, accept_set_bool -&gt; s</span>
        <span class="k">if</span> <span class="n">depth</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
            <span class="c1"># Take single leapfrog step in the given direction (direction * stepsize)</span>
            <span class="n">position_bar</span><span class="p">,</span> <span class="n">momentum_bar</span><span class="p">,</span> <span class="n">candidate_set_size</span><span class="p">,</span> <span class="n">accept_set_bool</span> <span class="o">=</span>\
                <span class="bp">self</span><span class="o">.</span><span class="n">_initalize_tree</span><span class="p">(</span><span class="n">position</span><span class="p">,</span> <span class="n">momentum</span><span class="p">,</span> <span class="n">slice_var</span><span class="p">,</span> <span class="n">direction</span> <span class="o">*</span> <span class="n">stepsize</span><span class="p">)</span>

            <span class="k">return</span> <span class="p">(</span><span class="n">position_bar</span><span class="p">,</span> <span class="n">momentum_bar</span><span class="p">,</span> <span class="n">position_bar</span><span class="p">,</span> <span class="n">momentum_bar</span><span class="p">,</span> <span class="n">position_bar</span><span class="p">,</span>
                    <span class="n">candidate_set_size</span><span class="p">,</span> <span class="n">accept_set_bool</span><span class="p">)</span>

        <span class="k">else</span><span class="p">:</span>
            <span class="c1"># Build left and right subtrees</span>
            <span class="p">(</span><span class="n">position_backward</span><span class="p">,</span> <span class="n">momentum_backward</span><span class="p">,</span> <span class="n">position_forward</span><span class="p">,</span> <span class="n">momentum_forward</span><span class="p">,</span> <span class="n">position_bar</span><span class="p">,</span>
             <span class="n">candidate_set_size</span><span class="p">,</span> <span class="n">accept_set_bool</span><span class="p">)</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_build_tree</span><span class="p">(</span><span class="n">position</span><span class="p">,</span> <span class="n">momentum</span><span class="p">,</span>
                                                                     <span class="n">slice_var</span><span class="p">,</span> <span class="n">direction</span><span class="p">,</span> <span class="n">depth</span> <span class="o">-</span> <span class="mi">1</span><span class="p">,</span> <span class="n">stepsize</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">accept_set_bool</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">direction</span> <span class="o">==</span> <span class="o">-</span><span class="mi">1</span><span class="p">:</span>
                    <span class="c1"># Build tree in backward direction</span>
                    <span class="p">(</span><span class="n">position_backward</span><span class="p">,</span> <span class="n">momentum_backward</span><span class="p">,</span> <span class="n">_</span><span class="p">,</span> <span class="n">_</span><span class="p">,</span> <span class="n">position_bar2</span><span class="p">,</span> <span class="n">candidate_set_size2</span><span class="p">,</span>
                     <span class="n">accept_set_bool2</span><span class="p">)</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_build_tree</span><span class="p">(</span><span class="n">position_backward</span><span class="p">,</span> <span class="n">momentum_backward</span><span class="p">,</span>
                                                          <span class="n">slice_var</span><span class="p">,</span> <span class="n">direction</span><span class="p">,</span> <span class="n">depth</span> <span class="o">-</span> <span class="mi">1</span><span class="p">,</span> <span class="n">stepsize</span><span class="p">)</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="c1"># Build tree in forward direction</span>
                    <span class="p">(</span><span class="n">_</span><span class="p">,</span> <span class="n">_</span><span class="p">,</span> <span class="n">position_forward</span><span class="p">,</span> <span class="n">momentum_forward</span><span class="p">,</span> <span class="n">position_bar2</span><span class="p">,</span> <span class="n">candidate_set_size2</span><span class="p">,</span>
                     <span class="n">accept_set_bool2</span><span class="p">)</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_build_tree</span><span class="p">(</span><span class="n">position_forward</span><span class="p">,</span> <span class="n">momentum_forward</span><span class="p">,</span>
                                                          <span class="n">slice_var</span><span class="p">,</span> <span class="n">direction</span><span class="p">,</span> <span class="n">depth</span> <span class="o">-</span> <span class="mi">1</span><span class="p">,</span> <span class="n">stepsize</span><span class="p">)</span>

                <span class="k">if</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">rand</span><span class="p">()</span> <span class="o">&lt;</span> <span class="n">candidate_set_size2</span> <span class="o">/</span> <span class="p">(</span><span class="n">candidate_set_size2</span> <span class="o">+</span> <span class="n">candidate_set_size</span><span class="p">):</span>
                    <span class="n">position_bar</span> <span class="o">=</span> <span class="n">position_bar2</span>

                <span class="n">accept_set_bool</span><span class="p">,</span> <span class="n">candidate_set_size</span> <span class="o">=</span>\
                    <span class="bp">self</span><span class="o">.</span><span class="n">_update_acceptance_criteria</span><span class="p">(</span><span class="n">position_forward</span><span class="p">,</span> <span class="n">position_backward</span><span class="p">,</span> <span class="n">momentum_forward</span><span class="p">,</span>
                                                     <span class="n">momentum_backward</span><span class="p">,</span> <span class="n">accept_set_bool2</span><span class="p">,</span> <span class="n">candidate_set_size</span><span class="p">,</span>
                                                     <span class="n">candidate_set_size2</span><span class="p">)</span>

            <span class="k">return</span> <span class="p">(</span><span class="n">position_backward</span><span class="p">,</span> <span class="n">momentum_backward</span><span class="p">,</span> <span class="n">position_forward</span><span class="p">,</span> <span class="n">momentum_forward</span><span class="p">,</span>
                    <span class="n">position_bar</span><span class="p">,</span> <span class="n">candidate_set_size</span><span class="p">,</span> <span class="n">accept_set_bool</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_sample</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">position</span><span class="p">,</span> <span class="n">stepsize</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns a sample using a single iteration of NUTS</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="c1"># Re-sampling momentum</span>
        <span class="n">momentum</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">normal</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="n">position</span><span class="p">))</span>

        <span class="c1"># Initializations</span>
        <span class="n">depth</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="n">position_backward</span><span class="p">,</span> <span class="n">position_forward</span> <span class="o">=</span> <span class="n">position</span><span class="p">,</span> <span class="n">position</span>
        <span class="n">momentum_backward</span><span class="p">,</span> <span class="n">momentum_forward</span> <span class="o">=</span> <span class="n">momentum</span><span class="p">,</span> <span class="n">momentum</span>
        <span class="n">candidate_set_size</span> <span class="o">=</span> <span class="n">accept_set_bool</span> <span class="o">=</span> <span class="mi">1</span>
        <span class="n">_</span><span class="p">,</span> <span class="n">log_pdf</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">grad_log_pdf</span><span class="p">(</span><span class="n">position</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="p">)</span><span class="o">.</span><span class="n">get_gradient_log_pdf</span><span class="p">()</span>

        <span class="c1"># Resample slice variable `u`</span>
        <span class="n">slice_var</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">uniform</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">exp</span><span class="p">(</span><span class="n">log_pdf</span> <span class="o">-</span> <span class="mf">0.5</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">dot</span><span class="p">(</span><span class="n">momentum</span><span class="p">,</span> <span class="n">momentum</span><span class="p">)))</span>

        <span class="k">while</span> <span class="n">accept_set_bool</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
            <span class="n">direction</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">choice</span><span class="p">([</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">],</span> <span class="n">p</span><span class="o">=</span><span class="p">[</span><span class="mf">0.5</span><span class="p">,</span> <span class="mf">0.5</span><span class="p">])</span>
            <span class="k">if</span> <span class="n">direction</span> <span class="o">==</span> <span class="o">-</span><span class="mi">1</span><span class="p">:</span>
                <span class="c1"># Build a tree in backward direction</span>
                <span class="p">(</span><span class="n">position_backward</span><span class="p">,</span> <span class="n">momentum_backward</span><span class="p">,</span> <span class="n">_</span><span class="p">,</span> <span class="n">_</span><span class="p">,</span> <span class="n">position_bar</span><span class="p">,</span>
                 <span class="n">candidate_set_size2</span><span class="p">,</span> <span class="n">accept_set_bool2</span><span class="p">)</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_build_tree</span><span class="p">(</span><span class="n">position_backward</span><span class="p">,</span> <span class="n">momentum_backward</span><span class="p">,</span>
                                                                           <span class="n">slice_var</span><span class="p">,</span> <span class="n">direction</span><span class="p">,</span> <span class="n">depth</span><span class="p">,</span> <span class="n">stepsize</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="c1"># Build tree in forward direction</span>
                <span class="p">(</span><span class="n">_</span><span class="p">,</span> <span class="n">_</span><span class="p">,</span> <span class="n">position_forward</span><span class="p">,</span> <span class="n">momentum_forward</span><span class="p">,</span> <span class="n">position_bar</span><span class="p">,</span>
                 <span class="n">candidate_set_size2</span><span class="p">,</span> <span class="n">accept_set_bool2</span><span class="p">)</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_build_tree</span><span class="p">(</span><span class="n">position_forward</span><span class="p">,</span> <span class="n">momentum_forward</span><span class="p">,</span>
                                                                           <span class="n">slice_var</span><span class="p">,</span> <span class="n">direction</span><span class="p">,</span> <span class="n">depth</span><span class="p">,</span> <span class="n">stepsize</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">accept_set_bool2</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">rand</span><span class="p">()</span> <span class="o">&lt;</span> <span class="n">candidate_set_size2</span> <span class="o">/</span> <span class="n">candidate_set_size</span><span class="p">:</span>
                    <span class="n">position</span> <span class="o">=</span> <span class="n">position_bar</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>

            <span class="n">accept_set_bool</span><span class="p">,</span> <span class="n">candidate_set_size</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_update_acceptance_criteria</span><span class="p">(</span><span class="n">position_forward</span><span class="p">,</span> <span class="n">position_backward</span><span class="p">,</span>
                                                                                   <span class="n">momentum_forward</span><span class="p">,</span> <span class="n">momentum_backward</span><span class="p">,</span>
                                                                                   <span class="n">accept_set_bool2</span><span class="p">,</span> <span class="n">candidate_set_size</span><span class="p">,</span>
                                                                                   <span class="n">candidate_set_size2</span><span class="p">)</span>
            <span class="n">depth</span> <span class="o">+=</span> <span class="mi">1</span>

        <span class="k">return</span> <span class="n">position</span>

<div class="viewcode-block" id="NoUTurnSampler.sample"><a class="viewcode-back" href="../../../sampling.html#pgmpy.sampling.NUTS.NoUTurnSampler.sample">[docs]</a>    <span class="k">def</span> <span class="nf">sample</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">initial_pos</span><span class="p">,</span> <span class="n">num_samples</span><span class="p">,</span> <span class="n">stepsize</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">return_type</span><span class="o">=</span><span class="s1">&#39;dataframe&#39;</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Method to return samples using No U Turn Sampler</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        initial_pos: A 1d array like object</span>
<span class="sd">            Vector representing values of parameter position, the starting</span>
<span class="sd">            state in markov chain.</span>

<span class="sd">        num_samples: int</span>
<span class="sd">            Number of samples to be generated</span>

<span class="sd">        stepsize: float , defaults to None</span>
<span class="sd">            The stepsize for proposing new values of position and momentum in simulate_dynamics</span>
<span class="sd">            If None, then will be choosen suitably</span>

<span class="sd">        return_type: string (dataframe | recarray)</span>
<span class="sd">            Return type for samples, either of &#39;dataframe&#39; or &#39;recarray&#39;.</span>
<span class="sd">            Defaults to &#39;dataframe&#39;</span>

<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        sampled: A pandas.DataFrame or a numpy.recarray object depending upon return_type argument</span>


<span class="sd">        Examples</span>
<span class="sd">        ---------</span>
<span class="sd">        &gt;&gt;&gt; from pgmpy.sampling import NoUTurnSampler as NUTS, GradLogPDFGaussian, LeapFrog</span>
<span class="sd">        &gt;&gt;&gt; from pgmpy.factors.continuous import GaussianDistribution as JGD</span>
<span class="sd">        &gt;&gt;&gt; import numpy as np</span>
<span class="sd">        &gt;&gt;&gt; mean = np.array([0, 0, 0])</span>
<span class="sd">        &gt;&gt;&gt; covariance = np.array([[6, 0.7, 0.2], [0.7, 3, 0.9], [0.2, 0.9, 1]])</span>
<span class="sd">        &gt;&gt;&gt; model = JGD([&#39;x&#39;, &#39;y&#39;, &#39;z&#39;], mean, covariance)</span>
<span class="sd">        &gt;&gt;&gt; sampler = NUTS(model=model, grad_log_pdf=GradLogPDFGaussian, simulate_dynamics=LeapFrog)</span>
<span class="sd">        &gt;&gt;&gt; samples = sampler.sample(initial_pos=np.array([1, 1, 1]), num_samples=10,</span>
<span class="sd">        ...                          stepsize=0.4, return_type=&#39;dataframe&#39;)</span>
<span class="sd">        &gt;&gt;&gt; samples</span>
<span class="sd">                  x         y         z</span>
<span class="sd">        0  1.000000  1.000000  1.000000</span>
<span class="sd">        1  1.760756  0.271543 -0.613309</span>
<span class="sd">        2  1.883387  0.990745 -0.611720</span>
<span class="sd">        3  0.980812  0.340336 -0.916283</span>
<span class="sd">        4  0.781338  0.647220 -0.948640</span>
<span class="sd">        5  0.040308 -1.391406  0.412201</span>
<span class="sd">        6  1.179549 -1.450552  1.105216</span>
<span class="sd">        7  1.100320 -1.313926  1.207815</span>
<span class="sd">        8  1.484520 -1.349247  0.768599</span>
<span class="sd">        9  0.934942 -1.894589  0.471772</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">initial_pos</span> <span class="o">=</span> <span class="n">_check_1d_array_object</span><span class="p">(</span><span class="n">initial_pos</span><span class="p">,</span> <span class="s1">&#39;initial_pos&#39;</span><span class="p">)</span>
        <span class="n">_check_length_equal</span><span class="p">(</span><span class="n">initial_pos</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">variables</span><span class="p">,</span> <span class="s1">&#39;initial_pos&#39;</span><span class="p">,</span> <span class="s1">&#39;model.variables&#39;</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">stepsize</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">stepsize</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_find_reasonable_stepsize</span><span class="p">(</span><span class="n">initial_pos</span><span class="p">)</span>

        <span class="n">types</span> <span class="o">=</span> <span class="p">[(</span><span class="n">var_name</span><span class="p">,</span> <span class="s1">&#39;float&#39;</span><span class="p">)</span> <span class="k">for</span> <span class="n">var_name</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">variables</span><span class="p">]</span>
        <span class="n">samples</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span><span class="n">num_samples</span><span class="p">,</span> <span class="n">dtype</span><span class="o">=</span><span class="n">types</span><span class="p">)</span><span class="o">.</span><span class="n">view</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">recarray</span><span class="p">)</span>

        <span class="n">samples</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="nb">tuple</span><span class="p">(</span><span class="n">initial_pos</span><span class="p">)</span>
        <span class="n">position_m</span> <span class="o">=</span> <span class="n">initial_pos</span>

        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="n">num_samples</span><span class="p">):</span>
            <span class="c1"># Genrating sample</span>
            <span class="n">position_m</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_sample</span><span class="p">(</span><span class="n">position_m</span><span class="p">,</span> <span class="n">stepsize</span><span class="p">)</span>
            <span class="n">samples</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="n">position_m</span>

        <span class="k">return</span> <span class="n">_return_samples</span><span class="p">(</span><span class="n">return_type</span><span class="p">,</span> <span class="n">samples</span><span class="p">)</span></div>

<div class="viewcode-block" id="NoUTurnSampler.generate_sample"><a class="viewcode-back" href="../../../sampling.html#pgmpy.sampling.NUTS.NoUTurnSampler.generate_sample">[docs]</a>    <span class="k">def</span> <span class="nf">generate_sample</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">initial_pos</span><span class="p">,</span> <span class="n">num_samples</span><span class="p">,</span> <span class="n">stepsize</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns a generator type object whose each iteration yields a sample</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        initial_pos: A 1d array like object</span>
<span class="sd">            Vector representing values of parameter position, the starting</span>
<span class="sd">            state in markov chain.</span>

<span class="sd">        num_samples: int</span>
<span class="sd">            Number of samples to be generated</span>

<span class="sd">        stepsize: float , defaults to None</span>
<span class="sd">            The stepsize for proposing new values of position and momentum in simulate_dynamics</span>
<span class="sd">            If None, then will be choosen suitably</span>

<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        generator: yielding a numpy.array type object for a sample</span>

<span class="sd">        Examples</span>
<span class="sd">        ---------</span>
<span class="sd">        &gt;&gt;&gt; from pgmpy.sampling import NoUTurnSampler as NUTS, GradLogPDFGaussian</span>
<span class="sd">        &gt;&gt;&gt; from pgmpy.factors.continuous import GaussianDistribution as JGD</span>
<span class="sd">        &gt;&gt;&gt; import numpy as np</span>
<span class="sd">        &gt;&gt;&gt; mean = np.array([11, -6])</span>
<span class="sd">        &gt;&gt;&gt; covariance = np.array([[0.7, 0.2], [0.2, 14]])</span>
<span class="sd">        &gt;&gt;&gt; model = JGD([&#39;x&#39;, &#39;y&#39;], mean, covariance)</span>
<span class="sd">        &gt;&gt;&gt; sampler = NUTS(model=model, grad_log_pdf=GradLogPDFGaussian)</span>
<span class="sd">        &gt;&gt;&gt; samples = sampler.generate_sample(initial_pos=np.array([1, 1]), num_samples=10, stepsize=0.4)</span>
<span class="sd">        &gt;&gt;&gt; samples = np.array([sample for sample in samples])</span>
<span class="sd">        &gt;&gt;&gt; samples</span>
<span class="sd">        array([[ 10.26357538,   0.10062725],</span>
<span class="sd">               [ 12.70600336,   0.63392499],</span>
<span class="sd">               [ 10.95523217,  -0.62079273],</span>
<span class="sd">               [ 10.66263031,  -4.08135962],</span>
<span class="sd">               [ 10.59255762,  -8.48085076],</span>
<span class="sd">               [  9.99860242,  -9.47096032],</span>
<span class="sd">               [ 10.5733564 ,  -9.83504745],</span>
<span class="sd">               [ 11.51302059,  -9.49919523],</span>
<span class="sd">               [ 11.31892143,  -8.5873259 ],</span>
<span class="sd">               [ 11.29008667,  -0.43809674]])</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">initial_pos</span> <span class="o">=</span> <span class="n">_check_1d_array_object</span><span class="p">(</span><span class="n">initial_pos</span><span class="p">,</span> <span class="s1">&#39;initial_pos&#39;</span><span class="p">)</span>
        <span class="n">_check_length_equal</span><span class="p">(</span><span class="n">initial_pos</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">variables</span><span class="p">,</span> <span class="s1">&#39;initial_pos&#39;</span><span class="p">,</span> <span class="s1">&#39;model.variables&#39;</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">stepsize</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">stepsize</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_find_reasonable_stepsize</span><span class="p">(</span><span class="n">initial_pos</span><span class="p">)</span>

        <span class="n">position_m</span> <span class="o">=</span> <span class="n">initial_pos</span>

        <span class="k">for</span> <span class="n">_</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">num_samples</span><span class="p">):</span>

            <span class="n">position_m</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_sample</span><span class="p">(</span><span class="n">position_m</span><span class="p">,</span> <span class="n">stepsize</span><span class="p">)</span>

            <span class="k">yield</span> <span class="n">position_m</span></div></div>


<div class="viewcode-block" id="NoUTurnSamplerDA"><a class="viewcode-back" href="../../../sampling.html#pgmpy.sampling.NUTS.NoUTurnSamplerDA">[docs]</a><span class="k">class</span> <span class="nc">NoUTurnSamplerDA</span><span class="p">(</span><span class="n">NoUTurnSampler</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Class for performing sampling in Continuous model</span>
<span class="sd">    using No U Turn sampler with dual averaging for</span>
<span class="sd">    adaptation of parameter stepsize.</span>

<span class="sd">    Parameters:</span>
<span class="sd">    -----------</span>
<span class="sd">    model: An instance pgmpy.models</span>
<span class="sd">        Model from which sampling has to be done</span>

<span class="sd">    grad_log_pdf: A subclass of pgmpy.inference.continuous.GradientLogPDF</span>
<span class="sd">        Class to compute the log and gradient log of distribution</span>

<span class="sd">    simulate_dynamics: A subclass of pgmpy.inference.continuous.BaseSimulateHamiltonianDynamics</span>
<span class="sd">        Class to propose future states of position and momentum in time by simulating</span>
<span class="sd">        HamiltonianDynamics</span>

<span class="sd">    delta: float (in between 0 and 1), defaults to 0.65</span>
<span class="sd">        The target HMC acceptance probability</span>

<span class="sd">    Public Methods:</span>
<span class="sd">    ---------------</span>
<span class="sd">    sample()</span>
<span class="sd">    generate_sample()</span>

<span class="sd">    Example:</span>
<span class="sd">    --------</span>
<span class="sd">    &gt;&gt;&gt; from pgmpy.sampling import NoUTurnSamplerDA as NUTSda, GradLogPDFGaussian</span>
<span class="sd">    &gt;&gt;&gt; from pgmpy.factors.continuous import GaussianDistribution as JGD</span>
<span class="sd">    &gt;&gt;&gt; import numpy as np</span>
<span class="sd">    &gt;&gt;&gt; mean = np.array([-1, 12, -3])</span>
<span class="sd">    &gt;&gt;&gt; covariance = np.array([[-2, 7, 2], [7, 14, 4], [2, 4, -1]])</span>
<span class="sd">    &gt;&gt;&gt; model = JGD([&#39;x&#39;, &#39;v&#39;, &#39;t&#39;], mean, covariance)</span>
<span class="sd">    &gt;&gt;&gt; sampler = NUTSda(model=model, grad_log_pdf=GradLogPDFGaussian)</span>
<span class="sd">    &gt;&gt;&gt; samples = sampler.sample(initial_pos=np.array([0, 0, 0]), num_adapt=10, num_samples=10,</span>
<span class="sd">    ...                          stepsize=0.25, return_type=&#39;recarray&#39;)</span>
<span class="sd">    &gt;&gt;&gt; samples</span>
<span class="sd">    rec.array([(0.0, 0.0, 0.0),</span>
<span class="sd">     (0.06100992691638076, -0.17118088764170125, 0.14048470935160887),</span>
<span class="sd">     (0.06100992691638076, -0.17118088764170125, 0.14048470935160887),</span>
<span class="sd">     (-0.7451883138013118, 1.7975387358691155, 2.3090698721374436),</span>
<span class="sd">     (-0.6207457594500309, 1.4611049498441024, 2.5890867012835574),</span>
<span class="sd">     (0.24043604780911487, 1.8660976216530618, 3.2508715592645347),</span>
<span class="sd">     (0.21509819341468212, 2.157760225367607, 3.5749582768731476),</span>
<span class="sd">     (0.20699150582681913, 2.0605044285377305, 3.8588980251618135),</span>
<span class="sd">     (0.20699150582681913, 2.0605044285377305, 3.8588980251618135),</span>
<span class="sd">     (0.085332419611991, 1.7556171374575567, 4.49985082288814)],</span>
<span class="sd">              dtype=[(&#39;x&#39;, &#39;&lt;f8&#39;), (&#39;v&#39;, &#39;&lt;f8&#39;), (&#39;t&#39;, &#39;&lt;f8&#39;)])</span>

<span class="sd">    References</span>
<span class="sd">    ----------</span>
<span class="sd">    Matthew D. Hoffman, Andrew Gelman, The No-U-Turn Sampler: Adaptively</span>
<span class="sd">    Setting Path Lengths in Hamiltonian Monte Carlo. Journal of</span>
<span class="sd">    Machine Learning Research 15 (2014) 1351-1381</span>
<span class="sd">    Algorithm 6 : No-U-Turn Sampler with Dual Averaging</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">model</span><span class="p">,</span> <span class="n">grad_log_pdf</span><span class="p">,</span> <span class="n">simulate_dynamics</span><span class="o">=</span><span class="n">LeapFrog</span><span class="p">,</span> <span class="n">delta</span><span class="o">=</span><span class="mf">0.65</span><span class="p">):</span>

        <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">delta</span><span class="p">,</span> <span class="nb">float</span><span class="p">)</span> <span class="ow">or</span> <span class="n">delta</span> <span class="o">&gt;</span> <span class="mf">1.0</span> <span class="ow">or</span> <span class="n">delta</span> <span class="o">&lt;</span> <span class="mf">0.0</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span>
                <span class="s2">&quot;delta should be a floating value in between 0 and 1&quot;</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">delta</span> <span class="o">=</span> <span class="n">delta</span>

        <span class="nb">super</span><span class="p">(</span><span class="n">NoUTurnSamplerDA</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">__init__</span><span class="p">(</span><span class="n">model</span><span class="o">=</span><span class="n">model</span><span class="p">,</span> <span class="n">grad_log_pdf</span><span class="o">=</span><span class="n">grad_log_pdf</span><span class="p">,</span>
                                               <span class="n">simulate_dynamics</span><span class="o">=</span><span class="n">simulate_dynamics</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_build_tree</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">position</span><span class="p">,</span> <span class="n">momentum</span><span class="p">,</span> <span class="n">slice_var</span><span class="p">,</span> <span class="n">direction</span><span class="p">,</span> <span class="n">depth</span><span class="p">,</span> <span class="n">stepsize</span><span class="p">,</span> <span class="n">position0</span><span class="p">,</span> <span class="n">momentum0</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Recursively builds a tree for proposing new position and momentum</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">depth</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>

            <span class="n">position_bar</span><span class="p">,</span> <span class="n">momentum_bar</span><span class="p">,</span> <span class="n">candidate_set_size</span><span class="p">,</span> <span class="n">accept_set_bool</span> <span class="o">=</span>\
                <span class="bp">self</span><span class="o">.</span><span class="n">_initalize_tree</span><span class="p">(</span><span class="n">position</span><span class="p">,</span> <span class="n">momentum</span><span class="p">,</span> <span class="n">slice_var</span><span class="p">,</span> <span class="n">direction</span> <span class="o">*</span> <span class="n">stepsize</span><span class="p">)</span>

            <span class="n">alpha</span> <span class="o">=</span> <span class="nb">min</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_acceptance_prob</span><span class="p">(</span><span class="n">position</span><span class="p">,</span> <span class="n">position_bar</span><span class="p">,</span> <span class="n">momentum</span><span class="p">,</span> <span class="n">momentum_bar</span><span class="p">))</span>

            <span class="k">return</span> <span class="p">(</span><span class="n">position_bar</span><span class="p">,</span> <span class="n">momentum_bar</span><span class="p">,</span> <span class="n">position_bar</span><span class="p">,</span> <span class="n">momentum_bar</span><span class="p">,</span> <span class="n">position_bar</span><span class="p">,</span>
                    <span class="n">candidate_set_size</span><span class="p">,</span> <span class="n">accept_set_bool</span><span class="p">,</span> <span class="n">alpha</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>

        <span class="k">else</span><span class="p">:</span>
            <span class="p">(</span><span class="n">position_backward</span><span class="p">,</span> <span class="n">momentum_backward</span><span class="p">,</span> <span class="n">position_forward</span><span class="p">,</span> <span class="n">momentum_forward</span><span class="p">,</span> <span class="n">position_bar</span><span class="p">,</span>
             <span class="n">candidate_set_size</span><span class="p">,</span> <span class="n">accept_set_bool</span><span class="p">,</span> <span class="n">alpha</span><span class="p">,</span> <span class="n">n_alpha</span><span class="p">)</span> <span class="o">=</span>\
                <span class="bp">self</span><span class="o">.</span><span class="n">_build_tree</span><span class="p">(</span><span class="n">position</span><span class="p">,</span> <span class="n">momentum</span><span class="p">,</span> <span class="n">slice_var</span><span class="p">,</span>
                                 <span class="n">direction</span><span class="p">,</span> <span class="n">depth</span> <span class="o">-</span> <span class="mi">1</span><span class="p">,</span> <span class="n">stepsize</span><span class="p">,</span> <span class="n">position0</span><span class="p">,</span> <span class="n">momentum0</span><span class="p">)</span>

            <span class="k">if</span> <span class="n">accept_set_bool</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">direction</span> <span class="o">==</span> <span class="o">-</span><span class="mi">1</span><span class="p">:</span>
                    <span class="c1"># Build tree in backward direction</span>
                    <span class="p">(</span><span class="n">position_backward</span><span class="p">,</span> <span class="n">momentum_backward</span><span class="p">,</span> <span class="n">_</span><span class="p">,</span> <span class="n">_</span><span class="p">,</span> <span class="n">position_bar2</span><span class="p">,</span> <span class="n">candidate_set_size2</span><span class="p">,</span> <span class="n">accept_set_bool2</span><span class="p">,</span>
                     <span class="n">alpha2</span><span class="p">,</span> <span class="n">n_alpha2</span><span class="p">)</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_build_tree</span><span class="p">(</span><span class="n">position_backward</span><span class="p">,</span> <span class="n">momentum_backward</span><span class="p">,</span> <span class="n">slice_var</span><span class="p">,</span> <span class="n">direction</span><span class="p">,</span>
                                                          <span class="n">depth</span> <span class="o">-</span> <span class="mi">1</span><span class="p">,</span> <span class="n">stepsize</span><span class="p">,</span> <span class="n">position0</span><span class="p">,</span> <span class="n">momentum0</span><span class="p">)</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="c1"># Build tree in forward direction</span>
                    <span class="p">(</span><span class="n">_</span><span class="p">,</span> <span class="n">_</span><span class="p">,</span> <span class="n">position_forward</span><span class="p">,</span> <span class="n">momentum_forward</span><span class="p">,</span> <span class="n">position_bar2</span><span class="p">,</span> <span class="n">candidate_set_size2</span><span class="p">,</span> <span class="n">accept_set_bool2</span><span class="p">,</span>
                     <span class="n">alpha2</span><span class="p">,</span> <span class="n">n_alpha2</span><span class="p">)</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_build_tree</span><span class="p">(</span><span class="n">position_forward</span><span class="p">,</span> <span class="n">momentum_forward</span><span class="p">,</span> <span class="n">slice_var</span><span class="p">,</span> <span class="n">direction</span><span class="p">,</span>
                                                          <span class="n">depth</span> <span class="o">-</span> <span class="mi">1</span><span class="p">,</span> <span class="n">stepsize</span><span class="p">,</span> <span class="n">position0</span><span class="p">,</span> <span class="n">momentum0</span><span class="p">)</span>

                <span class="k">if</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">rand</span><span class="p">()</span> <span class="o">&lt;</span> <span class="n">candidate_set_size2</span> <span class="o">/</span> <span class="p">(</span><span class="n">candidate_set_size2</span> <span class="o">+</span> <span class="n">candidate_set_size</span><span class="p">):</span>
                    <span class="n">position_bar</span> <span class="o">=</span> <span class="n">position_bar2</span>

                <span class="n">alpha</span> <span class="o">+=</span> <span class="n">alpha2</span>
                <span class="n">n_alpha</span> <span class="o">+=</span> <span class="n">n_alpha2</span>
                <span class="n">accept_set_bool</span><span class="p">,</span> <span class="n">candidate_set_size</span> <span class="o">=</span>\
                    <span class="bp">self</span><span class="o">.</span><span class="n">_update_acceptance_criteria</span><span class="p">(</span><span class="n">position_forward</span><span class="p">,</span> <span class="n">position_backward</span><span class="p">,</span> <span class="n">momentum_forward</span><span class="p">,</span>
                                                     <span class="n">momentum_backward</span><span class="p">,</span> <span class="n">accept_set_bool2</span><span class="p">,</span> <span class="n">candidate_set_size</span><span class="p">,</span>
                                                     <span class="n">candidate_set_size2</span><span class="p">)</span>

            <span class="k">return</span> <span class="p">(</span><span class="n">position_backward</span><span class="p">,</span> <span class="n">momentum_backward</span><span class="p">,</span> <span class="n">position_forward</span><span class="p">,</span> <span class="n">momentum_forward</span><span class="p">,</span> <span class="n">position_bar</span><span class="p">,</span>
                    <span class="n">candidate_set_size</span><span class="p">,</span> <span class="n">accept_set_bool</span><span class="p">,</span> <span class="n">alpha</span><span class="p">,</span> <span class="n">n_alpha</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_sample</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">position</span><span class="p">,</span> <span class="n">stepsize</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns a sample using a single iteration of NUTS with dual averaging</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="c1"># Re-sampling momentum</span>
        <span class="n">momentum</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">normal</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="n">position</span><span class="p">))</span>

        <span class="c1"># Initializations</span>
        <span class="n">depth</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="n">position_backward</span><span class="p">,</span> <span class="n">position_forward</span> <span class="o">=</span> <span class="n">position</span><span class="p">,</span> <span class="n">position</span>
        <span class="n">momentum_backward</span><span class="p">,</span> <span class="n">momentum_forward</span> <span class="o">=</span> <span class="n">momentum</span><span class="p">,</span> <span class="n">momentum</span>
        <span class="n">candidate_set_size</span> <span class="o">=</span> <span class="n">accept_set_bool</span> <span class="o">=</span> <span class="mi">1</span>
        <span class="n">position_m_1</span> <span class="o">=</span> <span class="n">position</span>
        <span class="n">_</span><span class="p">,</span> <span class="n">log_pdf</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">grad_log_pdf</span><span class="p">(</span><span class="n">position</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="p">)</span><span class="o">.</span><span class="n">get_gradient_log_pdf</span><span class="p">()</span>

        <span class="c1"># Resample slice variable `u`</span>
        <span class="n">slice_var</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">uniform</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">exp</span><span class="p">(</span><span class="n">log_pdf</span> <span class="o">-</span> <span class="mf">0.5</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">dot</span><span class="p">(</span><span class="n">momentum</span><span class="p">,</span> <span class="n">momentum</span><span class="p">)))</span>

        <span class="k">while</span> <span class="n">accept_set_bool</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
            <span class="n">direction</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">choice</span><span class="p">([</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">],</span> <span class="n">p</span><span class="o">=</span><span class="p">[</span><span class="mf">0.5</span><span class="p">,</span> <span class="mf">0.5</span><span class="p">])</span>
            <span class="k">if</span> <span class="n">direction</span> <span class="o">==</span> <span class="o">-</span><span class="mi">1</span><span class="p">:</span>
                <span class="c1"># Build a tree in backward direction</span>
                <span class="p">(</span><span class="n">position_backward</span><span class="p">,</span> <span class="n">momentum_backward</span><span class="p">,</span> <span class="n">_</span><span class="p">,</span> <span class="n">_</span><span class="p">,</span> <span class="n">position_bar</span><span class="p">,</span> <span class="n">candidate_set_size2</span><span class="p">,</span> <span class="n">accept_set_bool2</span><span class="p">,</span>
                 <span class="n">alpha</span><span class="p">,</span> <span class="n">n_alpha</span><span class="p">)</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_build_tree</span><span class="p">(</span><span class="n">position_backward</span><span class="p">,</span> <span class="n">momentum_backward</span><span class="p">,</span> <span class="n">slice_var</span><span class="p">,</span> <span class="n">direction</span><span class="p">,</span>
                                                    <span class="n">depth</span><span class="p">,</span> <span class="n">stepsize</span><span class="p">,</span> <span class="n">position_m_1</span><span class="p">,</span> <span class="n">momentum</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="c1"># Build tree in forward direction</span>
                <span class="p">(</span><span class="n">_</span><span class="p">,</span> <span class="n">_</span><span class="p">,</span> <span class="n">position_forward</span><span class="p">,</span> <span class="n">momentum_forward</span><span class="p">,</span> <span class="n">position_bar</span><span class="p">,</span> <span class="n">candidate_set_size2</span><span class="p">,</span> <span class="n">accept_set_bool2</span><span class="p">,</span>
                 <span class="n">alpha</span><span class="p">,</span> <span class="n">n_alpha</span><span class="p">)</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_build_tree</span><span class="p">(</span><span class="n">position_forward</span><span class="p">,</span> <span class="n">momentum_forward</span><span class="p">,</span> <span class="n">slice_var</span><span class="p">,</span> <span class="n">direction</span><span class="p">,</span>
                                                    <span class="n">depth</span><span class="p">,</span> <span class="n">stepsize</span><span class="p">,</span> <span class="n">position_m_1</span><span class="p">,</span> <span class="n">momentum</span><span class="p">)</span>

            <span class="k">if</span> <span class="n">accept_set_bool2</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">rand</span><span class="p">()</span> <span class="o">&lt;</span> <span class="n">candidate_set_size2</span> <span class="o">/</span> <span class="n">candidate_set_size</span><span class="p">:</span>
                    <span class="n">position</span> <span class="o">=</span> <span class="n">position_bar</span>

            <span class="n">accept_set_bool</span><span class="p">,</span> <span class="n">candidate_set_size</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_update_acceptance_criteria</span><span class="p">(</span><span class="n">position_forward</span><span class="p">,</span> <span class="n">position_backward</span><span class="p">,</span>
                                                                                   <span class="n">momentum_forward</span><span class="p">,</span> <span class="n">momentum_backward</span><span class="p">,</span>
                                                                                   <span class="n">accept_set_bool2</span><span class="p">,</span> <span class="n">candidate_set_size</span><span class="p">,</span>
                                                                                   <span class="n">candidate_set_size2</span><span class="p">)</span>

            <span class="n">depth</span> <span class="o">+=</span> <span class="mi">1</span>

        <span class="k">return</span> <span class="n">position</span><span class="p">,</span> <span class="n">alpha</span><span class="p">,</span> <span class="n">n_alpha</span>

<div class="viewcode-block" id="NoUTurnSamplerDA.sample"><a class="viewcode-back" href="../../../sampling.html#pgmpy.sampling.NUTS.NoUTurnSamplerDA.sample">[docs]</a>    <span class="k">def</span> <span class="nf">sample</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">initial_pos</span><span class="p">,</span> <span class="n">num_adapt</span><span class="p">,</span> <span class="n">num_samples</span><span class="p">,</span> <span class="n">stepsize</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">return_type</span><span class="o">=</span><span class="s1">&#39;dataframe&#39;</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns samples using No U Turn Sampler with dual averaging</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        initial_pos: A 1d array like object</span>
<span class="sd">            Vector representing values of parameter position, the starting</span>
<span class="sd">            state in markov chain.</span>

<span class="sd">        num_adapt: int</span>
<span class="sd">            The number of interations to run the adaptation of stepsize</span>

<span class="sd">        num_samples: int</span>
<span class="sd">            Number of samples to be generated</span>

<span class="sd">        stepsize: float , defaults to None</span>
<span class="sd">            The stepsize for proposing new values of position and momentum in simulate_dynamics</span>
<span class="sd">            If None, then will be choosen suitably</span>

<span class="sd">        return_type: string (dataframe | recarray)</span>
<span class="sd">            Return type for samples, either of &#39;dataframe&#39; or &#39;recarray&#39;.</span>
<span class="sd">            Defaults to &#39;dataframe&#39;</span>

<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        sampled: A pandas.DataFrame or a numpy.recarray object depending upon return_type argument</span>


<span class="sd">        Examples</span>
<span class="sd">        ---------</span>
<span class="sd">        &gt;&gt;&gt; from pgmpy.sampling import NoUTurnSamplerDA as NUTSda, GradLogPDFGaussian, LeapFrog</span>
<span class="sd">        &gt;&gt;&gt; from pgmpy.factors.continuous import GaussianDistribution as JGD</span>
<span class="sd">        &gt;&gt;&gt; import numpy as np</span>
<span class="sd">        &gt;&gt;&gt; mean = np.array([10, -13])</span>
<span class="sd">        &gt;&gt;&gt; covariance = np.array([[16, -3], [-3, 13]])</span>
<span class="sd">        &gt;&gt;&gt; model = JGD([&#39;x&#39;, &#39;y&#39;], mean, covariance)</span>
<span class="sd">        &gt;&gt;&gt; sampler = NUTSda(model=model, grad_log_pdf=GradLogPDFGaussian, simulate_dynamics=LeapFrog)</span>
<span class="sd">        &gt;&gt;&gt; samples = sampler.sample(initial_pos=np.array([12, -4]), num_adapt=10, num_samples=10,</span>
<span class="sd">        ...                          stepsize=0.1, return_type=&#39;dataframe&#39;)</span>
<span class="sd">        &gt;&gt;&gt; samples</span>
<span class="sd">                   x          y</span>
<span class="sd">        0  12.000000  -4.000000</span>
<span class="sd">        1  11.864821  -3.696109</span>
<span class="sd">        2  10.546986  -4.892169</span>
<span class="sd">        3   8.526596 -21.555793</span>
<span class="sd">        4   8.526596 -21.555793</span>
<span class="sd">        5  11.343194  -6.353789</span>
<span class="sd">        6  -1.583269 -12.802931</span>
<span class="sd">        7  12.411957 -11.704859</span>
<span class="sd">        8  13.253336 -20.169492</span>
<span class="sd">        9  11.295901  -7.665058</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">initial_pos</span> <span class="o">=</span> <span class="n">_check_1d_array_object</span><span class="p">(</span><span class="n">initial_pos</span><span class="p">,</span> <span class="s1">&#39;initial_pos&#39;</span><span class="p">)</span>
        <span class="n">_check_length_equal</span><span class="p">(</span><span class="n">initial_pos</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">variables</span><span class="p">,</span> <span class="s1">&#39;initial_pos&#39;</span><span class="p">,</span> <span class="s1">&#39;model.variables&#39;</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">stepsize</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">stepsize</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_find_reasonable_stepsize</span><span class="p">(</span><span class="n">initial_pos</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">num_adapt</span> <span class="o">&lt;=</span> <span class="mi">1</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">NoUTurnSampler</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">grad_log_pdf</span><span class="p">,</span>
                                  <span class="bp">self</span><span class="o">.</span><span class="n">simulate_dynamics</span><span class="p">)</span><span class="o">.</span><span class="n">sample</span><span class="p">(</span><span class="n">initial_pos</span><span class="p">,</span> <span class="n">num_samples</span><span class="p">,</span> <span class="n">stepsize</span><span class="p">)</span>

        <span class="n">mu</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">log</span><span class="p">(</span><span class="mf">10.0</span> <span class="o">*</span> <span class="n">stepsize</span><span class="p">)</span>
        <span class="n">stepsize_bar</span> <span class="o">=</span> <span class="mf">1.0</span>
        <span class="n">h_bar</span> <span class="o">=</span> <span class="mf">0.0</span>

        <span class="n">types</span> <span class="o">=</span> <span class="p">[(</span><span class="n">var_name</span><span class="p">,</span> <span class="s1">&#39;float&#39;</span><span class="p">)</span> <span class="k">for</span> <span class="n">var_name</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">variables</span><span class="p">]</span>
        <span class="n">samples</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span><span class="n">num_samples</span><span class="p">,</span> <span class="n">dtype</span><span class="o">=</span><span class="n">types</span><span class="p">)</span><span class="o">.</span><span class="n">view</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">recarray</span><span class="p">)</span>
        <span class="n">samples</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="nb">tuple</span><span class="p">(</span><span class="n">initial_pos</span><span class="p">)</span>
        <span class="n">position_m</span> <span class="o">=</span> <span class="n">initial_pos</span>

        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="n">num_samples</span><span class="p">):</span>

            <span class="n">position_m</span><span class="p">,</span> <span class="n">alpha</span><span class="p">,</span> <span class="n">n_alpha</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_sample</span><span class="p">(</span><span class="n">position_m</span><span class="p">,</span> <span class="n">stepsize</span><span class="p">)</span>
            <span class="n">samples</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="n">position_m</span>

            <span class="k">if</span> <span class="n">i</span> <span class="o">&lt;=</span> <span class="n">num_adapt</span><span class="p">:</span>
                <span class="n">stepsize</span><span class="p">,</span> <span class="n">stepsize_bar</span><span class="p">,</span> <span class="n">h_bar</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_adapt_params</span><span class="p">(</span><span class="n">stepsize</span><span class="p">,</span> <span class="n">stepsize_bar</span><span class="p">,</span> <span class="n">h_bar</span><span class="p">,</span> <span class="n">mu</span><span class="p">,</span>
                                                                   <span class="n">i</span><span class="p">,</span> <span class="n">alpha</span><span class="p">,</span> <span class="n">n_alpha</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">stepsize</span> <span class="o">=</span> <span class="n">stepsize_bar</span>

        <span class="k">return</span> <span class="n">_return_samples</span><span class="p">(</span><span class="n">return_type</span><span class="p">,</span> <span class="n">samples</span><span class="p">)</span></div>

<div class="viewcode-block" id="NoUTurnSamplerDA.generate_sample"><a class="viewcode-back" href="../../../sampling.html#pgmpy.sampling.NUTS.NoUTurnSamplerDA.generate_sample">[docs]</a>    <span class="k">def</span> <span class="nf">generate_sample</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">initial_pos</span><span class="p">,</span> <span class="n">num_adapt</span><span class="p">,</span> <span class="n">num_samples</span><span class="p">,</span> <span class="n">stepsize</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns a generator type object whose each iteration yields a sample</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        initial_pos: A 1d array like object</span>
<span class="sd">            Vector representing values of parameter position, the starting</span>
<span class="sd">            state in markov chain.</span>

<span class="sd">        num_adapt: int</span>
<span class="sd">            The number of interations to run the adaptation of stepsize</span>

<span class="sd">        num_samples: int</span>
<span class="sd">            Number of samples to be generated</span>

<span class="sd">        stepsize: float , defaults to None</span>
<span class="sd">            The stepsize for proposing new values of position and momentum in simulate_dynamics</span>
<span class="sd">            If None, then will be choosen suitably</span>

<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        genrator: yielding a numpy.array type object for a sample</span>

<span class="sd">        Examples</span>
<span class="sd">        --------</span>
<span class="sd">        &gt;&gt;&gt; from pgmpy.sampling import NoUTurnSamplerDA as NUTSda, GradLogPDFGaussian</span>
<span class="sd">        &gt;&gt;&gt; from pgmpy.factors.continuous import GaussianDistribution as JGD</span>
<span class="sd">        &gt;&gt;&gt; import numpy as np</span>
<span class="sd">        &gt;&gt;&gt; mean = np.array([1, -100])</span>
<span class="sd">        &gt;&gt;&gt; covariance = np.array([[-12, 45], [45, -10]])</span>
<span class="sd">        &gt;&gt;&gt; model = JGD([&#39;a&#39;, &#39;b&#39;], mean, covariance)</span>
<span class="sd">        &gt;&gt;&gt; sampler = NUTSda(model=model, grad_log_pdf=GradLogPDFGaussian, simulate_dynamics=LeapFrog)</span>
<span class="sd">        &gt;&gt;&gt; samples = sampler.generate_sample(initial_pos=np.array([12, -4]), num_adapt=10,</span>
<span class="sd">        ...                                   num_samples=10, stepsize=0.1)</span>
<span class="sd">        &gt;&gt;&gt; samples</span>
<span class="sd">        &lt;generator object NoUTurnSamplerDA.generate_sample at 0x7f4fed46a4c0&gt;</span>
<span class="sd">        &gt;&gt;&gt; samples_array = np.array([sample for sample in samples])</span>
<span class="sd">        &gt;&gt;&gt; samples_array</span>
<span class="sd">        array([[ 11.89963386,  -4.06572636],</span>
<span class="sd">               [ 10.3453755 ,  -7.5700289 ],</span>
<span class="sd">               [-26.56899659, -15.3920684 ],</span>
<span class="sd">               [-29.97143077, -12.0801625 ],</span>
<span class="sd">               [-29.97143077, -12.0801625 ],</span>
<span class="sd">               [-33.07960829,  -8.90440347],</span>
<span class="sd">               [-55.28263496, -17.31718524],</span>
<span class="sd">               [-55.28263496, -17.31718524],</span>
<span class="sd">               [-56.63440044, -16.03309364],</span>
<span class="sd">               [-63.880094  , -19.19981944]])</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">initial_pos</span> <span class="o">=</span> <span class="n">_check_1d_array_object</span><span class="p">(</span><span class="n">initial_pos</span><span class="p">,</span> <span class="s1">&#39;initial_pos&#39;</span><span class="p">)</span>
        <span class="n">_check_length_equal</span><span class="p">(</span><span class="n">initial_pos</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">variables</span><span class="p">,</span> <span class="s1">&#39;initial_pos&#39;</span><span class="p">,</span> <span class="s1">&#39;model.variables&#39;</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">stepsize</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">stepsize</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_find_reasonable_stepsize</span><span class="p">(</span><span class="n">initial_pos</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">num_adapt</span> <span class="o">&lt;=</span> <span class="mi">1</span><span class="p">:</span>  <span class="c1"># return sample generated using Simple HMC algorithm</span>
            <span class="k">for</span> <span class="n">sample</span> <span class="ow">in</span> <span class="n">NoUTurnSampler</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">grad_log_pdf</span><span class="p">,</span>
                                         <span class="bp">self</span><span class="o">.</span><span class="n">simulate_dynamics</span><span class="p">)</span><span class="o">.</span><span class="n">generate_sample</span><span class="p">(</span><span class="n">initial_pos</span><span class="p">,</span> <span class="n">num_samples</span><span class="p">,</span> <span class="n">stepsize</span><span class="p">):</span>
                <span class="k">yield</span> <span class="n">sample</span>
            <span class="k">return</span>
        <span class="n">mu</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">log</span><span class="p">(</span><span class="mf">10.0</span> <span class="o">*</span> <span class="n">stepsize</span><span class="p">)</span>

        <span class="n">stepsize_bar</span> <span class="o">=</span> <span class="mf">1.0</span>
        <span class="n">h_bar</span> <span class="o">=</span> <span class="mf">0.0</span>

        <span class="n">position_m</span> <span class="o">=</span> <span class="n">initial_pos</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>
        <span class="n">num_adapt</span> <span class="o">+=</span> <span class="mi">1</span>

        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="n">num_samples</span> <span class="o">+</span> <span class="mi">1</span><span class="p">):</span>

            <span class="n">position_m</span><span class="p">,</span> <span class="n">alpha</span><span class="p">,</span> <span class="n">n_alpha</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_sample</span><span class="p">(</span><span class="n">position_m</span><span class="p">,</span> <span class="n">stepsize</span><span class="p">)</span>

            <span class="k">if</span> <span class="n">i</span> <span class="o">&lt;=</span> <span class="n">num_adapt</span><span class="p">:</span>
                <span class="n">stepsize</span><span class="p">,</span> <span class="n">stepsize_bar</span><span class="p">,</span> <span class="n">h_bar</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_adapt_params</span><span class="p">(</span><span class="n">stepsize</span><span class="p">,</span> <span class="n">stepsize_bar</span><span class="p">,</span> <span class="n">h_bar</span><span class="p">,</span> <span class="n">mu</span><span class="p">,</span>
                                                                   <span class="n">i</span><span class="p">,</span> <span class="n">alpha</span><span class="p">,</span> <span class="n">n_alpha</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">stepsize</span> <span class="o">=</span> <span class="n">stepsize_bar</span>

            <span class="k">yield</span> <span class="n">position_m</span></div></div>
</pre></div>

          </div>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="nav-item nav-item-0"><a href="../../../index.html">pgmpy 0.1.2 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="../../index.html" >Module code</a> &#187;</li> 
      </ul>
    </div>
    <div class="footer" role="contentinfo">
        &#169; Copyright 2016, Ankur Ankan.
      Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.5.1.
    </div>
  </body>
</html>